﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Attribute;
using WebUI.Models;
using WebUI.HtmlHelpers;
using DomainModel;
using Utils;
using WebUI.Utils;

namespace WebUI.Controllers
{
    [AdminAuthorize]
    public class AdminController : BaseController
    {
        public ActionResult Index()
        {
            return View(new AdminIndexViewModel());
        }

        [HttpGet]
        public ActionResult EmailTemplate()
        {
            return View(new EmailTemplateListViewModel());
        }

        [HttpGet]
        public ActionResult EditEmailTemplate(int emailTemplateID)
        {
            return View(new EmailTemplateEditViewModel(emailTemplateID));
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditEmailTemplate(EmailTemplateEditViewModel model, int sendTestEmail)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DataUtils.UpdateEmailTemplate(
                        model.EmailTemplate.EmailTemplateID, 
                        model.EmailTemplate.EmailTemplateType, 
                        model.EmailTemplate.EmailTemplateName,
                        model.EmailTemplate.Subject, 
                        model.EmailTemplate.Body, 
                        model.EmailTemplate.BodyParameter);

                    if (sendTestEmail == 1)
                    { 

                        EmailUtils.Send(
                            GlobalUtilities.GetInstance().Email_NoReply, 
                            GlobalUtilities.GetInstance().Email_BCC, 
                            (EmailTemplateType)model.EmailTemplate.EmailTemplateType, 
                            CommonUtilities.GetEmailTemplateBodyParameterArray(model.EmailTemplate.BodyParameter), 0, 0, string.Empty, false);
                    }

                    base.CreateSessionMessageForSuccess("update succeed");
                }
                catch
                {
                    base.CreateSessionMessageForError("update failed, db error");
                }
                return View(model);
            }
            else
            {
                base.CreateSessionMessageForError("update failed, modelstate is invalid");
                return View(model);
            }
        }

        [HttpGet]
        public ActionResult DeleteEmailTemplate(int emailTemplateID)
        {
            DataUtils.DeleteEmailTemplate(emailTemplateID);
            return RedirectToAction("EmailTemplate");
        }

        [HttpGet]
        public ActionResult CreateEmailTemplate()
        {
            return View(new EmailTemplateCreateViewModel());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateEmailTemplate(EmailTemplateCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DataUtils.InsertEmailTemplate(
                        model.EmailTemplate.EmailTemplateName,
                        model.EmailTemplate.Subject,
                        model.EmailTemplate.Body,
                        model.EmailTemplate.BodyParameter);

                    base.CreateSessionMessageForSuccess("create succeed");
                }
                catch
                {
                    base.CreateSessionMessageForError("create failed, db error");
                }
                return View(model);
            }
            else
            {
                base.CreateSessionMessageForError("create failed, modelstate is invalid");
                return View(model);
            }
        }

        public ActionResult CreateJobTokenCookie(int jobid)
        {
            if (jobid <= 0)
            {
                return Content("please specify jobid");
            }
            else
            {
                Job job = DataUtils.GetOneFromText<Job>("select * from job where jobid = @JobID and status = 0", new { jobid });
                if (job != null && job.JobID > 0)
                {
                    base.CreateCookie(ConstUtils.C_JobToken, job.JobToken);
                    return Content("created cookie");
                }
                else
                {
                    return Content("cant' find this job, may its status not 0 or can't find the jobid");
                }
            }
        }

        public ActionResult DeleteJobTokenCookie()
        {
            base.DeleteCookie(ConstUtils.C_JobToken);
            return Content("Done");
        }
    }
}
